package cn.wggf.dev.qcgg.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.wggf.dev.qcgg.dao.QcggDao;
import cn.wggf.dev.qcgg.dao.impl.QcggDaoImpl;
import cn.wggf.dev.qcgg.pojo.Qcgg;
import cn.wggf.dev.qcgg.pojo.QcggZp;
import cn.wggf.dev.qcgg.service.QcggService;
import cn.wggf.dev.qcgg.util.Base64Image;
import cn.wggf.dev.qcgg.util.UploadUtil;

/**
 * 汽车公告业务接口实现类
 * 
 * @author Yubo Jia
 *
 *         2018年1月10日
 */
public class QcggServiceImpl implements QcggService {

	private QcggDao qcggDao = new QcggDaoImpl();

	/**
	 * 根据条件查询车辆公告
	 */
	public List<Map<String, Object>> getQcggByConditions(String PP, String GGXH) {
		List<Map<String, Object>> qcggList = qcggDao.getQcggByConditions(PP, GGXH);
		List<Map<String, Object>> qcggList1 = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : qcggList) {
			Date date = (Date) map.get("createtime");
			String qccs = (String) map.get("QCCS");
			String qccs1 = qccs.replaceAll("\r|\n|\t", "");
			// String qccs1 = qccs.replaceAll(" ", "");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String createtime = sdf.format(date);
			// System.out.println("转化为字符串》》》》》》》》》》》"+createtime);
			// System.out.println("数据库》》》》》》》》》》》》》》》》》》》》》》"+map.get("createtime"));
			// System.out.println("去除空格换行》》》》》》》》》》》"+qccs1);
			// System.out.println("数据库》》》》》》》》》》》》》》》》》》》》》》"+map.get("QCCS"));
			map.put("createtime", createtime);
			map.put("QCCS", qccs1);
			qcggList1.add(map);
		}
		return qcggList1;
	}

	/**
	 * 根据ID查询汽车公告
	 */
	public List<Map<String, Object>> getQcggById(Integer ID) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> qcggEY = qcggDao.getQcggById(ID);
		if (!qcggEY.isEmpty()) {
			Map<String, Object> map = qcggEY.get(0);
			String str = "D:\\jyb\\statics\\";
			List<QcggZp> list = new ArrayList<QcggZp>();
			String p1 = (String) map.get("ZP1");
			String p2 = (String) map.get("ZP2");
			String p3 = (String) map.get("ZP3");
			String p4 = (String) map.get("ZP4");
			String p5 = (String) map.get("ZP5");
			String p6 = (String) map.get("ZP6");
			QcggZp qcggZp1 = new QcggZp("zp1", p1);
			QcggZp qcggZp2 = new QcggZp("zp2", p2);
			QcggZp qcggZp3 = new QcggZp("zp3", p3);
			QcggZp qcggZp4 = new QcggZp("zp4", p4);
			QcggZp qcggZp5 = new QcggZp("zp5", p5);
			QcggZp qcggZp6 = new QcggZp("zp6", p6);
			list.add(qcggZp1);
			list.add(qcggZp2);
			list.add(qcggZp3);
			list.add(qcggZp4);
			list.add(qcggZp5);
			list.add(qcggZp6);
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getPath() != null && !"".equals(list.get(i).getPath())) {
					if ("<".equals(String.valueOf(list.get(i).getPath().charAt(0)))) {
						int o = list.get(i).getPath().indexOf('"') + 1;// 第一个'号+1下标
						int y = list.get(i).getPath().indexOf('"', o + 1);// 第二个'号下标
						// 从字符串截取出图片url
						String fileName = list.get(i).getPath().substring(o, y);
						UploadUtil up = new UploadUtil();
						// 保存路径
						String savePath = str + list.get(i).getKey() + map.get("GGPC") + map.get("GGXH") + map.get("ID")
								+ ".jpg";
						boolean a = up.saveUrlAs(fileName, savePath);
						if (a) {
							String path = list.get(i).getKey() + map.get("GGPC") + map.get("GGXH") + map.get("ID")
									+ ".jpg";
							list.get(i).setPath(path);
						} else {
						}
					}
				}
			}
			int in = qcggDao.updateQcggById(ID, list);
			String Pat = "D:\\apache-tomcat-7.0.82\\wtpwebapps\\QCGG1\\statics\\uploadfiles\\";
			for (int i = 0; i < list.size(); i++) {
				if ((list.get(i).getPath()) != null && !"null".equals(list.get(i).getPath())
						&& !"".equals(list.get(i).getPath())
						&& !"<".equals(String.valueOf(list.get(i).getPath().charAt(0)))) {
					File srcFile = new File(str + list.get(i).getPath());
					String src = Pat + list.get(i).getPath();
					File destFile = new File(src);
					String sys = "D:\\apache-tomcat-7.0.82\\wtpwebapps\\QCGG1\\statics\\uploadfiles\\" + list.get(i).getPath();
					list.get(i).setPath(sys);
					try {
						BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
						BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
						byte[] bys = new byte[1024];
						int len = 0;
						while ((len = bis.read(bys)) != -1) {
							bos.write(bys, 0, len);
							bos.flush();
						}
						bos.close();
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					if (list.get(i).getKey().equals("zp1")) {
						//map.put("ZP1", list.get(i).getPath());
						map.put("ZP1", Base64Image.GetImageStr(list.get(i).getPath()));
					} else if (list.get(i).getKey().equals("zp2")) {
						map.put("ZP2", Base64Image.GetImageStr(list.get(i).getPath()));
					} else if (list.get(i).getKey().equals("zp3")) {
						map.put("ZP3", Base64Image.GetImageStr(list.get(i).getPath()));
					} else if (list.get(i).getKey().equals("zp4")) {
						map.put("ZP4", Base64Image.GetImageStr(list.get(i).getPath()));
					} else if (list.get(i).getKey().equals("zp5")) {
						map.put("ZP5", Base64Image.GetImageStr(list.get(i).getPath()));
					} else if (list.get(i).getKey().equals("zp6")) {
						map.put("ZP6", Base64Image.GetImageStr(list.get(i).getPath()));
					}
				}
			}
			map.remove("GGPC");
			map.remove("GGXH");
			qcggEY.set(0, map);
			return qcggEY;
		} else {
			return null;
		}
	}
}
